#ifndef DAVMOVE_H
#define DAVMOVE_H



#include "base_modul.h"
#include "qxml.h"
#include "basedav.h"
#include "networkdav.h"


class NetworkDav;
    
class DavMove : public QObject, public Base_Modul
{
    Q_OBJECT
//
public: 
    DavMove( NetworkDav * db , const QString sourcepath , const QString dest )
{
    loopnummer = -1;
    pathregister = sourcepath;
    actualurl = sourcepath;
    davinfo = new QHttp();
    davinfo->setHost(db->Host(),db->Port());
    davinfo->setUser(db->User(),db->Pass());
    QString desturl = QString("http://%1%2").arg(db->Host()).arg(dest);
    qt_unlink(LASTMOVEACTION);
    qt_unlink(MOVESTREAM);
    
    qDebug() << "#### actualurl  ->" << actualurl; 
    qDebug() << "#### desturl  ->" << desturl; 
    
    QString wandxml ="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
    wandxml.append("<d:propertybehavior xmlns:d='DAV:'>\n");
    wandxml.append("<d:keepalive>*</d:keepalive>\n");
    wandxml.append("</d:propertybehavior>\n");
    bool conne = fwriteutf8(MOVESTREAM,wandxml);
    qDebug() << "#### strem write   ->" << MOVESTREAM << "-" << conne; 
    
    BeamUpFile = new QFile(MOVESTREAM);  /* send comand file */
    BeamUpFile->open(QIODevice::ReadOnly);
    BeamLogFile = new QFile(LASTMOVEACTION);     /* responder file info */
    BeamLogFile->open(QIODevice::ReadWrite);
    
    QHttpRequestHeader header("MOVE", actualurl ,1,1);   /* header */
    header.setValue("Host", db->Host());
    header.setValue("Destination",desturl);
    header.setValue("Overwrite","T");
   //////// header.setValue("If","(<opaquelocktoken:fe184f2e-6eec-41d0-c765-01adc56e6bb4>) (<opaquelocktoken:e454f3f3-acdc-452a-56c7-00a5c91e4b77>)");
    ///////header.setValue("Connection", "keep-alive");
    header.setValue("User-Agent", WEBAGENTNAME );
    header.setValue("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    header.setValue("Accept-Language", "it,de-de;q=0.8,it-it;q=0.6,en-us;q=0.4,en;q=0.2");
    header.setValue("Content-Type", "text/xml; charset=\"utf-8\"" );
    david = davinfo->request(header,BeamUpFile,BeamLogFile);
    connect(davinfo, SIGNAL(requestFinished(int, bool)), this, SLOT(Wake_Finish(int, bool)));   
}
protected:
private:
  QString pathinfofile;
  QHttp *davinfo;
  QFile *BeamUpFile;
  QFile *BeamLogFile;
  int david;
  int loopnummer;
  int statuscode;
  QString actualurl;
  QString pathregister;
  QString xmlinfo;
  QString dummypath;
  QString DisplayName;
signals:
  void WarningMsg( QString e );
  void MoveDirReady(); 
public slots:

void  Wake_Finish( int requestId, bool error )
{
   qDebug() << "#### Wake_Finish...  ->"; 
   if (requestId != david ) {
     return;
   }
    QHttpResponseHeader lastheader = davinfo->lastResponse();
    statuscode = lastheader.statusCode();
   
    davinfo->close();
    BeamLogFile->close();
    BeamUpFile->close();
   

   
    if (error) {
    emit WarningMsg(tr("Error on url: %1\nErrorMsg %2").arg(actualurl).arg(davinfo->errorString()));
    return;
    } 
    
    if (statuscode != 201 ) {
    emit WarningMsg(tr("Error on url: %1\nErrorMsg %2\nHttp Header code respond: %3").arg(actualurl).arg(davinfo->errorString()).arg(statuscode));  
    return;   
    }
    
    ///////////////qDebug() << "#### HEADER STATUS CODE  ->" << statuscode;   
    emit MoveDirReady();
}

};

	
	







#endif // DAVMOVE_H

